package persistence.Impl;

import domain.CartItem;
import persistence.CartDAO;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

public class CartDAOImpl implements CartDAO {
    private static final String getCartItemByUserName = "select * from shoppingcart where username = ?";
    private static final String getQuantityByItemIdAndUserName = "select quantity from shoppingcart where username = ? and itemid = ?";
    private static final String insertToCart="insert into shoppingcart (username,itemid,quantity,inStock) values (?,?,1,1)";
    private static final String removeItemFromCart = "delete from shoppingcart where username = ? and itemid = ?";
    private static final String updateQuantityByUserName = "update shoppingcart set quantity = ? where username = ? and itemid = ?";

    @Override
    public List<CartItem> getCartItemIdByUserName(String UserName) {
        List<CartItem> CartItemList = new ArrayList<CartItem>();
        ItemDAOImpl itemDAO = new ItemDAOImpl();
        try {
            Connection connection = DBUtil.getConnection();
            PreparedStatement preparedStatement = connection.prepareStatement(getCartItemByUserName);
            preparedStatement.setString(1,UserName);
            ResultSet resultSet = preparedStatement.executeQuery();
            while(resultSet.next()){
                CartItem cartItem= new CartItem();
                cartItem.setQuantity(resultSet.getInt("quantity"));
                cartItem.setItem(itemDAO.getItem(resultSet.getString("itemid")));
                CartItemList.add(cartItem);
            }
            DBUtil.close(connection,preparedStatement,resultSet);
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return CartItemList;
    }

    @Override
    public int getQuantityByItemIdAndUserName(String UserName, String ItemId) {
        int Quantity = 1;
        try {
            Connection connection = DBUtil.getConnection();
            PreparedStatement preparedStatement = connection.prepareStatement(getQuantityByItemIdAndUserName);
            preparedStatement.setString(1,UserName);
            preparedStatement.setString(2,ItemId);
            ResultSet resultSet = preparedStatement.executeQuery();
            if(resultSet.next()) {
                Quantity = resultSet.getInt(1);
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return Quantity;
    }

    @Override
    public void insertToCart(String UserName,String ItemId) {
        try {
            Connection connection = DBUtil.getConnection();
            PreparedStatement preparedStatement = connection.prepareStatement(insertToCart);
            preparedStatement.setString(1,UserName);
            preparedStatement.setString(2,ItemId);
            preparedStatement.executeUpdate();
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }

    @Override
    public void removeItemByUserName(String UserName, String ItemId) {
        try {
            Connection connection = DBUtil.getConnection();
            PreparedStatement preparedStatement = connection.prepareStatement(removeItemFromCart);
            preparedStatement.setString(1,UserName);
            preparedStatement.setString(2,ItemId);
            preparedStatement.executeUpdate();
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }

    @Override
    public void updateQuantityByUserName(String UserName, String ItemId, int Quantity) {
        try {
            Connection connection = DBUtil.getConnection();
            PreparedStatement preparedStatement = connection.prepareStatement(updateQuantityByUserName);
            preparedStatement.setInt(1,Quantity);
            preparedStatement.setString(2,UserName);
            preparedStatement.setString(3,ItemId);
            preparedStatement.executeUpdate();
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }
}
